<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="角色管理">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <script>
        jQuery(function () {
        });

        fixTableOperatorButton();
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;
            renderTable();

            table.on('tool(operator)', function (obj) {
                switch(obj.event){
                    case 'edit':
                        edit(obj.data);
                        break;
                    case 'delete':
                        del(obj.data);
                        break;
                    case 'permission':
                        openTab("设置权限",'${baseUrl}user/role/rolePermission.action?roleId='+obj.data.id);
                        break;
                }
            });

            //监听事件
            table.on('toolbar(operator)', function(obj){
                switch(obj.event){
                    case 'add':
                        add();
                        break;
                };
            });
        });
        
        function renderTable() {
            layui.table.render({
                id:'userTable',
                elem: '#operator',
                url: "${baseUrl}user/role/getRoles.action?time="+(new Date()).valueOf(),
                toolbar: '#tableToolBar',
                defaultToolbar:[],
                cols: [[
                    {type: "numbers", width: 30},
                    {field: 'id', width: 10, title: 'ID',hide:true},
                    {field: 'name', width: 110, title: '名称',style:"font-size:12px;"},
                    {field: 'description', minWidth: 200, title: '描述', style:"font-size:12px;"},
                    {title: '操作', fixed:'right', templet: '#operBar'},
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                },
                done: function(res, curr, count){
                }
            });
        }

        function add() {
            prompt2("新增角色","角色名称","角色描述", function (index,name,description) {
                if (name == null || name == "") {
                    tip("角色名称不能为空");
                    return;
                }
                var data = {
                    name:  name
                    ,description:description
                };
                post("${baseUrl}user/role/add.action",data, function (resp) {
                    if (resp.code == 1){
                        tip("新增成功");
                        layer.close(index);
                        renderTable();
                    } else {
                        tip(resp.msg);
                    }
                });
            });
        }

        function del(data) {
            layer.confirm('删除角色《'+data.name+'》?', {icon: 3, title:'删除角色,所有用户的此角色也将被删除'}, function(index){
                layer.load();
                post("${baseUrl}user/role/delete.action",data, function (resp) {
                    if (resp.code == 1){
                        tip("删除成功");
                        layer.closeAll();
                        renderTable();
                    } else {
                        tip(resp.msg);
                    }
                });
            });
        }

        function edit(data) {
            prompt2_val("编辑角色",data.name,"角色名称",data.description,"角色描述",function (index,name,description) {
                if (name == null || name == "") {
                    tip("角色名称不能为空");
                    return;
                }
                var json = {
                    id:data.id
                    ,name:  name
                    ,description:description
                    ,companyId : data.companyId
                };

                var checkName = true;
                if (name == data.name) {
                    checkName = false;
                }
                layer.load();
                post("${baseUrl}user/role/update.action?checkName="+checkName,json, function (resp) {
                    if (resp.code == 1){
                        tip("更新成功");
                        layer.closeAll();
                        renderTable();
                    } else {
                        tip(resp.msg);
                    }
                });
            });
        }

    </script>

</html:header>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <table class="layui-hide" id="operator" lay-filter="operator" lay-size="sm"></table>

        <script type="text/html" id="tableToolBar">
            <div class="layui-inline">
                <a class="layui-btn" lay-event="add">新增</a>
            </div>
        </script>

        <script type="text/html" id="operBar">
            {{#  if(d.name != '管理员'){ }}
<%--                <a class="layui-btn layui-btn-xs layui-btn-normal data-count-edit" href="javascript:;" data-iframe-tab="${baseUrl}user/role/rolePermission.action?roleId={{d.id}}" data-title="权限设置-{{d.name}}" data-icon="fa fa-lock">--%>
<%--                    权限--%>
<%--                </a>--%>
                <a class="layui-btn layui-btn-xs layui-btn-normal table-operator" lay-event="permission">权限</a>
                <a class="layui-btn layui-btn-xs  table-operator" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger  table-operator" lay-event="delete">删除</a>
            {{#  } }}
        </script>
    </div>
</div>
</body>
</html>